import { execute } from './utils/javascript.template'
import { Push } from './interface/evaluate-function.type'
import { BrowserWindow } from 'electron'

class Driver {
  win
  constructor(win: BrowserWindow) {
    this.win = win
  }

  private async evaluate_now<T, K extends any[], R>(
    fn: (...args: Push<K, T>) => R,
    ...args: K
  ): Promise<R> {
    let _args = Array.prototype.slice
      .call(arguments)
      .slice(1)
      .map((argument) => {
        return { argument: JSON.stringify(argument) }
      })

    let stringFn = String(fn)
    let source = execute({ fn: stringFn, args: _args })
    debugger

    return await this.win.webContents.executeJavaScript(source, true)
  }

  async evaluate<T, K extends any[], R>(fn: (...args: Push<K, T>) => R, ...args: K): Promise<R> {
    debugger
    return this.evaluate_now(fn, ...args)
  }
}

export { Driver }
